/**
 * Skills Demo
 *
 * Demonstrates how to use the Skills system
 */

// Set test environment to skip API key validation
process.env.NODE_ENV = 'test';

import { skillsManager } from '@/skills';
import { builtinSkills } from '@/skills/builtin';
import { logger } from '@/utils/logger';

async function main() {
  console.log('🎨 CAgent Skills Demo\n');

  // Register all built-in skills
  console.log('📦 Registering built-in skills...');
  skillsManager.registerSkills(builtinSkills);
  console.log(`✅ Registered ${builtinSkills.length} skills\n`);

  // List all skills
  console.log('📋 Available Skills:');
  const allSkills = skillsManager.getAllSkills();
  allSkills.forEach(skill => {
    console.log(`  - ${skill.name} (${skill.id})`);
    console.log(`    Category: ${skill.category}`);
    console.log(`    Tools: ${skill.tools.length}`);
    console.log(`    Description: ${skill.description}\n`);
  });

  // Get statistics
  console.log('📊 Statistics:');
  const stats = skillsManager.getStatistics();
  console.log(`  Total skills: ${stats.total}`);
  console.log(`  Active skills: ${stats.active}`);
  console.log(`  By category:`, stats.byCategory);
  console.log();

  // Activate Data Analyst skill
  console.log('🔧 Activating Data Analyst skill...');
  skillsManager.activateSkill('data-analyst');
  console.log('✅ Data Analyst skill activated\n');

  // Get active skills
  console.log('✨ Active Skills:');
  const activeSkills = skillsManager.getActiveSkills();
  activeSkills.forEach(skill => {
    console.log(`  - ${skill.name}`);
  });
  console.log();

  // Get active tools
  console.log('🛠️  Active Tools:');
  const activeTools = skillsManager.getActiveTools();
  activeTools.forEach(tool => {
    console.log(`  - ${tool.name}: ${tool.description}`);
  });
  console.log();

  // Get active system prompt
  console.log('📝 Active System Prompt:');
  const systemPrompt = skillsManager.getActiveSystemPrompt();
  console.log(systemPrompt.substring(0, 200) + '...\n');

  // Search skills
  console.log('🔍 Searching for "code" skills...');
  const searchResults = skillsManager.searchSkills('code');
  searchResults.forEach(skill => {
    console.log(`  - ${skill.name}: ${skill.description}`);
  });
  console.log();

  // Get skills by category
  console.log('📂 Skills in "data_analysis" category:');
  const dataSkills = skillsManager.getSkillsByCategory('data_analysis');
  dataSkills.forEach(skill => {
    console.log(`  - ${skill.name}`);
  });
  console.log();

  // Export a skill
  console.log('💾 Exporting Data Analyst skill...');
  const exported = skillsManager.exportSkill('data-analyst');
  if (exported) {
    console.log('✅ Skill exported successfully');
    console.log(`   Size: ${exported.length} bytes\n`);
  }

  // Deactivate skill
  console.log('🔴 Deactivating Data Analyst skill...');
  skillsManager.deactivateSkill('data-analyst');
  console.log('✅ Data Analyst skill deactivated\n');

  // Final statistics
  console.log('📊 Final Statistics:');
  const finalStats = skillsManager.getStatistics();
  console.log(`  Total skills: ${finalStats.total}`);
  console.log(`  Active skills: ${finalStats.active}`);
  console.log();

  console.log('🎉 Demo completed successfully!');
}

main().catch(error => {
  logger.error('Demo failed', error);
  process.exit(1);
});

